package com.hp.bon.sgw.snmp;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.Counter32;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TimeTicks;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

import com.hp.bon.sgw.Constants.CheckStatus;
import com.hp.bon.sgw.start.SpringContext;

public class SgwNMContext {
	private static final Logger logger = LoggerFactory.getLogger(SgwNMContext.class);

	private static Snmp snmp = null;
	private static CommunityTarget target = null;

	private static final long startTime = System.currentTimeMillis();

	public static void init() throws IOException {
		reloadCfg();
		snmp = new Snmp(new DefaultUdpTransportMapping());
	}

	public static void reloadCfg() {
		//
		String trapAddress = System.getProperty("snmptrapAddress");
		if (trapAddress == null) {
			trapAddress = "127.0.0.1/163";
			logger.warn("cannot find 'snmptrapAddress' , use default:" + trapAddress);
		}
		logger.info("snmptrapAddress :" + trapAddress);
		if (trapAddress != null) {
			Address targetAddress = GenericAddress.parse(trapAddress);
			CommunityTarget targetNew = new CommunityTarget();
			targetNew.setAddress(targetAddress);
			targetNew.setRetries(2);
			targetNew.setTimeout(1500);
			targetNew.setVersion(SnmpConstants.version2c);
			target = targetNew;
		}
	}

	public static void sendTrap(OID notifiyOID, VariableBinding[] vbs) {
		long cur = System.currentTimeMillis();
		PDU pdu = new PDU();
		pdu.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks((cur - startTime) / 10)));
		pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, notifiyOID));
		pdu.addAll(vbs);
		pdu.setType(PDU.TRAP);
		try {
			ResponseEvent respEvnt = snmp.send(pdu, target);
		} catch (IOException e) {
			logger.warn(e.getMessage(), e);
		}
		logger.info("Send trap to " + target.getAddress() + ",oid=" + notifiyOID);
	}
	
	/**
	 * 3.1.5.2.2 流量控制异常通知
	 */
	public static void notifyFlowSizeAlert(String nodeCode, String userName, String capability, long count, long size, String reason, long time) {
		// TODO
	}

	/**
	 * 接入SR失败
	 */
	public static void notifyBusiAlarm(String type, String value, CheckStatus status) {
    VariableBinding[] vbs = new VariableBinding[11];
    String prov = "";
    try {
      int pos1 = value.trim().indexOf("@");
      prov = value.trim().substring(pos1 + 1, pos1 + 4);
    }
    catch (Exception localException) {
    }
    vbs[0] = new VariableBinding(SgwNMConstants.BusiAlarm.NeId, new Counter32(SpringContext.getInstance().getConfigService().getIntValue("system.ne_type_id", 3013)));
    vbs[1] = new VariableBinding(SgwNMConstants.BusiAlarm.NodeId, new OctetString("0"));
    vbs[2] = new VariableBinding(SgwNMConstants.BusiAlarm.Prov, new OctetString(prov));
    vbs[3] = new VariableBinding(SgwNMConstants.BusiAlarm.Module, new OctetString(type.trim()));
    vbs[4] = new VariableBinding(SgwNMConstants.BusiAlarm.BusiClass, new OctetString(value));
    vbs[5] = new VariableBinding(SgwNMConstants.BusiAlarm.KpiFlag, new OctetString("1"));
    vbs[6] = new VariableBinding(SgwNMConstants.BusiAlarm.KpiValue, new OctetString("busi alarm"));
    vbs[7] = new VariableBinding(SgwNMConstants.BusiAlarm.KpiLevel, new OctetString("1"));
    vbs[8] = new VariableBinding(SgwNMConstants.BusiAlarm.AlarmContent, new OctetString(type + ":" + value + " status is " + status));
    vbs[9] = new VariableBinding(SgwNMConstants.BusiAlarm.ExpertAdvice, new OctetString(""));
    vbs[10] = new VariableBinding(SgwNMConstants.BusiAlarm.SysUpdateTime, new Counter32(System.currentTimeMillis() / 1000L));

    sendTrap(SgwNMConstants.BusiAlarm.Notification_OID, vbs);
  }
}